package com.acce.gxsc.page.outCar.feeDetail;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.loaclBean.LocalFeeCarDetailBean;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.EncodingHandler;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huxley on 2017/5/26.
 */
public class ToOutCarDetailActivity extends BaseMvpActivity<ToOutCarDetailView, ToOutCarDetailPresenter> implements ToOutCarDetailView {

    String inCarKey;
    @Bind(R.id.tv_id)
    TextView     mTvId;
    @Bind(R.id.tv_state)
    TextView     mTvState;
    @Bind(R.id.tv_time)
    TextView     mTvTime;
    @Bind(R.id.tv_car)
    TextView     mTvCar;
    @Bind(R.id.tv_customer)
    TextView     mTvCustomer;
    @Bind(R.id.tv_goods_info)
    TextView     mTvGoodsInfo;
    @Bind(R.id.tv_state_1)
    TextView     mTvState1;
    @Bind(R.id.tv_state_2)
    TextView     mTvState2;
    @Bind(R.id.tv_batch_id)
    TextView     mTvBatchId;
    @Bind(R.id.iv_bar_code)
    ImageView    mIvBarCode;
    @Bind(R.id.ll_content)
    LinearLayout mLlContent;
    @Bind(R.id.tv_out)
    TextView     mTvOut;
    @Bind(R.id.tv_car_type)
    TextView     mTvCarType;
    @Bind(R.id.tv_out_car_time)
    TextView     mTvOutCarTime;

    @Override
    protected ToOutCarDetailPresenter createPresenter() {
        return new ToOutCarDetailPresenter();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_out_car_detail);
        ButterKnife.bind(this);
        init();
        inCarKey = getIntent().getStringExtra("inCarKey");
        mPresenter.loadInCarDetail(inCarKey);
    }


    private void init() {
    }

    @Override
    public void printSuccess() {

    }

    LocalFeeCarDetailBean mLocalFeeCarDetailBean;

    @Override
    public void loadSuccess(final LocalFeeCarDetailBean result) {
        mLocalFeeCarDetailBean = result;
        if (!StringUtils.isEmpty(result.inCar.inCarId)) {
            if (result.charge != null && result.charge.size() > 0 && !StringUtils.isEmpty(result.charge.get(0).feeId)) {
                mTvId.setText(String.format("NO: %s", Tools.getStringNonNull(result.charge.get(0).feeId)));
            } else {
                mTvId.setText(String.format("NO: %s", Tools.getStringNonNull(result.inCar.inCarId)));
            }
            if ("1".equals(result.inCar.outCarStatus)) {
                mTvOut.setText("已离场");
                mTvOut.setBackgroundResource(R.color.color_e4e4e4);
                mTvOut.setEnabled(false);
                mTvState.setText("状态：已离场");
                mTvState.setTextColor(Tools.getColor(R.color.color_525695));
                mTvOutCarTime.setText(result.inCar.outCarTime.substring(0, 19));
            } else {
                mTvOut.setBackgroundResource(R.color.red);
                mTvOut.setEnabled(true);
                mTvState.setText("状态：未离场");
                mTvState.setTextColor(Tools.getColor(R.color.red));
            }
            mTvCarType.setText(Tools.getString1(result.inCar.carType, result.inCar.diliveryType));
            mTvTime.setText(result.inCar.createTime.substring(0, 19));
            mTvCar.setText(Tools.getString1(result.inCar.plateNum, result.inCar.containNo));
            mTvCustomer.setText(Tools.getString1(result.inCar.contactName, result.inCar.underCompany));
            mTvGoodsInfo.setText(Tools.getStringNonNull(result.inCar.goodsInfo));
            mTvBatchId.setText(String.format("BATCH:%s", result.inBatch.inBatchId));
            getWindow().getDecorView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Bitmap codeImage = null;
                    try {
                        codeImage = EncodingHandler.createBarCode128(result.inBatch.inBatchId, (int) (mLlContent.getWidth() * 1.5), (int) getResources().getDimension(R.dimen.x200));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (codeImage != null) {
                        mIvBarCode.setImageBitmap(codeImage);
                    }
                }
            }, 100);
            if (StringUtils.isEmpty(result.inBatch.fee2Id)) {
                mTvState1.setText("未知单据");
                mTvState1.setTextColor(Tools.getColor(R.color.yellow));
                mTvOut.setText("未知单据");
                mTvOut.setBackgroundResource(R.color.color_e4e4e4);
                mTvOut.setEnabled(false);
                mTvState.setText("状态：未知单据");
                mTvState.setTextColor(Tools.getColor(R.color.yellow));
            } else {
                switch (result.inBatch.fee2Id) {
                    case "0":
                        mTvState1.setText("不用收费");
                        mTvState1.setTextColor(Tools.getColor(R.color.yellow));
                        break;
                    case "1":
                        mTvState1.setText("未缴费");
                        mTvState1.setTextColor(Tools.getColor(R.color.red));
                        break;
                    case "33":
                        mTvState1.setText("未缴费（已打印通知单）");
                        mTvState1.setTextColor(Tools.getColor(R.color.red));
                        break;
                    case "55":
                        mTvState1.setText("已收费");
                        mTvState1.setTextColor(Tools.getColor(R.color.green));
                        break;
                    default:
                        mTvState1.setText("未知单据");
                        mTvState1.setTextColor(Tools.getColor(R.color.yellow));
                        break;
                }
            }
            if (StringUtils.isEmpty(result.inBatch.fee3Id)) {
                mTvState2.setText("未知单据");
                mTvState2.setTextColor(Tools.getColor(R.color.yellow));
                mTvOut.setText("未知单据");
                mTvOut.setBackgroundResource(R.color.color_e4e4e4);
                mTvOut.setEnabled(false);
                mTvState.setText("状态：未知单据");
                mTvState.setTextColor(Tools.getColor(R.color.yellow));
            } else {
                switch (result.inBatch.fee3Id) {
                    case "0":
                        mTvState2.setText("不用收费");
                        mTvState2.setTextColor(Tools.getColor(R.color.yellow));
                        break;
                    case "1":
                        mTvState2.setText("未缴费");
                        mTvState2.setTextColor(Tools.getColor(R.color.red));
                        break;
                    case "33":
                        mTvState2.setText("未缴费（已打印通知单）");
                        mTvState2.setTextColor(Tools.getColor(R.color.red));
                        break;
                    case "55":
                        mTvState2.setText("已收费");
                        mTvState2.setTextColor(Tools.getColor(R.color.green));
                        break;
                    default:
                        mTvState2.setText("未知单据");
                        mTvState2.setTextColor(Tools.getColor(R.color.yellow));
                        break;
                }
            }
        }
    }

    @Override
    public void toOutCarSuccess() {
        mTvOut.setText("已离场");
        mTvOut.setBackgroundResource(R.color.color_e4e4e4);
        mTvOut.setEnabled(false);
        mTvState.setText("状态：已离场");
        mTvState.setTextColor(Tools.getColor(R.color.color_525695));
    }

    @OnClick(R.id.tv_out)
    public void onClick() {
        if (StringUtils.isEmpty(mLocalFeeCarDetailBean.inCar.inCarId)) {
            ToastUtils.show("未找到该进场单！");
            return;
        }
        final AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
        final AlertDialog alertDialog1 = builder1
                .setTitle("提示")
                .setMessage("确定离场？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mPresenter.toOutCar(mLocalFeeCarDetailBean.inCar.inCarId, "");
                    }
                }).create();
        alertDialog1.setCanceledOnTouchOutside(false);
        alertDialog1.show();
    }
}
